import QtQuick 2.12
import QtQuick.Controls 2.12
import QtQuick.Layouts 1.12
import ConfigElementType 1.0
import "qrc:/qmlComponents/ConfigManager"
import "qrc:/qmlComponents/AxisModulePos"
import "qrc:/qmlComponents/VisionViewer"
import "qrc:/qmlComponents/CustomizedUIElement"

Item {
    function calibrateSTCaliGlass(){
        tem.runOnMultiObjects([[visionManager, "calibrate", "STCaliGlassCalibration"],
                               [bonderHead, "readSTCaliGlassPrOffset"]])
    }

    ColumnLayout{
        RowLayout{
            XYZModulePos{
                moduleName: "BondHeadXYZ"
                posName: "STPickPlaceCaliGlassPos"
            }
            XYAndZPos{
                title: "BWHOpticXYZ_CaliGlassPrPos"
                xyModuleName: "BWHOpticXY"
                zModuleName: "BWHOpticZ"
                xyPosName: "CaliGlassPrPos"
                zPosName: "CaliGlassPrPos"
                moveXyBtnVisible: false
                moveZBtnVisible: false
            }
        }

        RowLayout{
            spacing: 40

            ColumnLayout{
                Layout.alignment: Qt.AlignTop

                ConfigRow{
                    configModel: bhConfig
                    configName: "stCaliGlassPrOffsetX"
                    Component.onCompleted: {
                        init(ConfigElementType.Double)
                    }
                }
                ConfigRow{
                    configModel: bhConfig
                    configName: "stCaliGlassPrOffsetY"
                    Component.onCompleted: {
                        init(ConfigElementType.Double)
                    }
                }
                ConfigRow{
                    configModel: bhConfig
                    configName: "stCameraCenterXShift"
                    Component.onCompleted: {
                        init(ConfigElementType.Double)
                    }
                }

                VisionLocationViewer{
                    calibrationName: "STCaliGlassCalibration"
                    visionLocationName: "STCaliGlassLocation"
                    useCustomCalibrationFunc: true
                    customCalibrationFunc: calibrateSTCaliGlass
                }
            }

            ColumnLayout{
                Layout.alignment: Qt.AlignTop

                ConfigRow{
                    configModel: bhConfig
                    configName: "bhToSTCameraOffsetX"
                    Component.onCompleted: {
                        init(ConfigElementType.Double)
                    }
                }
                ConfigRow{
                    configModel: bhConfig
                    configName: "bhToSTCameraOffsetY"
                    Component.onCompleted: {
                        init(ConfigElementType.Double)
                    }
                }
                Button{
                    text: qsTr("EstimateBHToSTCameraOffset")
                    onClicked: {
                        tem.runSingleCmd(bonderHead, "estimateBHToSTCameraOffset")
                    }
                }

                GroupBox{
                    ColumnLayout{
                        RowLayout{
                            Label{
                                text: qsTr("TestTimes:")
                            }
                            TextField{
                                id: txtStaticDeviationTestTimes
                                text: "5"
                                validator: IntValidator{
                                    bottom: 1
                                }
                                implicitWidth: 80
                                selectByMouse: true
                            }
                        }
                        Button{
                            text: qsTr("STCaliGlassStaticPrDeviationTest")
                            onClicked: {
                                tem.runSingleCmd(bonderHead, "stCaliGlassStaticPrDeviationTest", [txtStaticDeviationTestTimes.text])
                            }
                        }
                        Button{
                            text: qsTr("STCaliGlassPickPlaceDeviationTest")
                            onClicked: {
                                tem.runSingleCmd(bonderHead, "stCaliGlassPickPlaceDeviationTest", [txtStaticDeviationTestTimes.text])
                            }
                        }
                        RowLayout{
                            Label{
                                text: qsTr("DelayAfterMoveToPickPos:")
                            }
                            TextField{
                                id: txtDelayAfterMoveToPickPos
                                text: "0"
                                validator: IntValidator{
                                    bottom: 1
                                }
                                implicitWidth: 80
                                selectByMouse: true
                            }
                        }
                        Button{
                            text: qsTr("STCaliGlassStaticPickPlaceDeviationTest")
                            onClicked: {
                                tem.runSingleCmd(bonderHead, "stCaliGlassStaticPickPlaceDeviationTest", [txtStaticDeviationTestTimes.text, txtDelayAfterMoveToPickPos.text])
                            }
                        }
                    }
                }
            }
        }
    }
}
